package jeecg.business.thread.task.impl;

import java.net.URLEncoder;
import java.util.Date;

import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.keyinfo.KeyinfoEntity;
import jeecg.business.entity.keyword.KeywordEntity;
import jeecg.business.entity.log.CollLogEntity;
import jeecg.business.entity.loginuser.LoginUserEntity;
import jeecg.business.entity.site.SiteEntity;
import jeecg.business.thread.task.ExecTask;

import org.apache.commons.lang.math.RandomUtils;
import org.jeecgframework.core.util.clean.SinaWeiboHtmlCleanUtil;
import org.jeecgframework.core.util.clean.bean.HtmlParseResult;

public class SinaWeiboExecTask extends ExecTask{
	
	
	protected int current = 1;
	
	public static final String QUERY_URL = "http://m.weibo.cn/page/pageJson?containerid=100103type%3D1%26q%3D{keyword}&type=all&page={page}";

	public SinaWeiboExecTask(EnterEntity enter , KeywordEntity keyword, SiteEntity site) {
		super(enter , keyword, site);
	}

	@Override
	public void run() {
		CollLogEntity log = null;
		log = saveLog();
		String homeUrl = site.getZddz();
		try{
			HtmlParseResult result = null;
			KeyinfoEntity keyinfo = null;
			//需要获取的页面
			boolean hasNext = true;
			
			//模拟登录
			String cookies = null;
			
			LoginUserEntity user = loginUserService.getRandom("xlwb");
//			cookies = SinaWeiboHtmlCleanUtil.loginSinaBlog4Html(user.getYhm(), user.getMm(), site.getNyzfj());
			
			//1	2 3
			while(current<maxpage&&hasNext){
				//替换关键值		
				
				homeUrl = QUERY_URL.replace(KEYWORD_FLAG ,URLEncoder.encode(URLEncoder.encode(keyword.getKeyword(),"utf-8"),"utf-8"));
				homeUrl = homeUrl.replace(PAGE_FLAG, current+"");
				String lyUrl = homeUrl;
				System.out.println("新浪微博："+homeUrl);
				result = SinaWeiboHtmlCleanUtil.fetchSinaBlog(homeUrl, site.getWyzfj(),site,cookies);
				
				if(current>=result.getMaxPage()) hasNext = false;
				
				if(result == null) return;					
				
				if(result.getChildResultList() != null){
					for(HtmlParseResult childResult : result.getChildResultList()){
						if(keyinfoService.hasHref(childResult.getUrl() , enter.getId())) continue;
						childResult.setType(site.getJclx());
						childResult.setCjzd(site.getMc());
						childResult.setLy(lyUrl);
	//					childResult.setSite(IcpUtil.getSiteFromUrl(lyUrl));
						childResult.setSite(site.getMc());
						childResult.setTitle(childResult.getBodycontent());
//						childResult.setIp(ipaddress.get("ip"));
//						childResult.setAddress(ipaddress.get("address"));
						childResult.setKeyword(keyword.getKeyword());
						keyinfo = new KeyinfoEntity(childResult);
						keyinfo.setQyid(enter.getId());
						if(filter(enter, keyinfo , keyword))		//过滤
							keyinfoService.saveKeyinfo(keyinfo);
					}
				}		
				
				current++;
//				homeUrl = SINA_PAGE_URL;
//				homeUrl = homeUrl.replaceAll("\\?", currPage+"");
//				homeUrl = homeUrl.replaceAll(KEYWORD_FLAG , URLEncoder.encode(URLEncoder.encode(target.getGjc(),"utf-8"),"utf-8"));			
				
//				Thread.sleep((15+RandomUtils.nextInt(10))*1000);
			}
//			log = collLogService.get(CollLogEntity.class, log.getId());
		}catch(Exception e){
//			e.printStackTrace();
//			log.setSfcg("2");
//			log.setCwxx(e.getMessage());
		}finally{
			log = collLogService.get(CollLogEntity.class, log.getId());
			log.setJssj(new Date());
			collLogService.updateEntitie(log);
			System.out.println("end....");
		}	
	}

}
